---
title: "Identifying Price Informativeness"
author: "Eduardo Davila^[Yale] & Cecilia Parlatore^[NYU Stern]"
date: "`r Sys.Date()`"
output:
  html_document: default
  pdf_document:  default
---

```{r echo=FALSE, include=FALSE}

library(here); library(tidyverse); library(cowplot); library(kableExtra); library(moments); library(DescTools)
path <- here::here(); print(path); setwd(path); rm(path)

load("intermediate/data_selected.RData");
load("intermediate/io_share.RData")
load("intermediate/parameters.RData")
load("intermediate/results_PS.RData")
load("intermediate/results_rolling_clean.RData")

source("functions/sc_ivol.R")

options(scipen = 999)

if (!file.exists("output/output_figures/")){ 
 dir.create("output/output_figures/") 
}

```

# Earnings Volatility Histogram

```{r}

df_earnings <- df_q %>% filter(year >= 1985) %>% group_by(permno) %>% 
    summarize(count = n(), 
              mean_payoff = mean(delta_log_payoff_growth, na.rm = T),
              std_payoff  = sd(delta_log_payoff_growth, na.rm = T)) %>% 
    mutate(log_std_payoff = log(std_payoff))

x_start <- -7
x_end   <- 0
x_step  <- 0.5
x_axis  <- seq(x_start, x_end, x_step)

y_start <- 0
y_end   <- 0.05
y_step  <- 0.01
y_axis  <- seq(y_start, y_end, y_step)

  print(paste(length(unique(df_earnings$permno)), mean(df_earnings$mean_payoff, na.rm=T), median(df_earnings$mean_payoff, na.rm=T)))

p <- df_earnings %>% 
  ggplot(aes(x = log_std_payoff)) +
  geom_histogram(aes(y = stat(count) / sum(count)), bins = 81, colour = "black", fill = "steelblue", alpha = 0.2) + 
  labs(x = "Log Standard Deviation of Earnings' Growth Rates", 
       y = "Frequency") +
  theme_classic() +
  theme(text = element_text(size = 16)) +
  scale_x_continuous(breaks = x_axis, expand = c(0.01, 0.01)) +
  scale_y_continuous(breaks = y_axis, expand = c(0.0, 0.0)) +
  coord_cartesian(xlim  = c(x_start, x_end), ylim = c(y_start, y_end))

ggsave(paste0("output/output_figures/figure_IA1.pdf"), plot = p, width = 12, height = 5, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA1.jpeg"), plot = p, width = 12, height = 5, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA1.eps"), plot = p, width = 12, height = 5, units = "in", dpi = 300)
  
```

# Summary Histogram

```{r}

histogram_fn <- function(input_df, namefile) {
  
  
  if(startsWith(namefile, "figure_2")){
    y_end <- 0.12
    y_step  <- 0.03
    x_end   <- 0.25
    x_step  <- 0.05
    x_expand <- c(0.005, 0.015)
    last_year <- 2015
  }else if(startsWith(namefile, "figure_7")){
    y_end <- 0.6
    y_step  <- 0.1
    x_end   <- 0.5
    x_step  <- 0.1
    x_expand <- c(0.005, 0.015)
    last_year <- 2012
  }else if(startsWith(namefile, "figure_IA10")){
    y_end <- 0.24
    y_step  <- 0.02
    x_end   <- 0.6
    x_step  <- 0.1
    x_expand <- c(0.005, 0.015)
    last_year <- 2015
  }else if(startsWith(namefile, "figure_IA15")){
    y_end <- 0.24
    y_step  <- 0.02
    x_end   <- 0.8
    x_step  <- 0.1
    x_expand <- c(0.005, 0.015)
    last_year <- 2015
  }else if(startsWith(namefile, "figure_IA17")){
    y_end <- 0.20
    y_step  <- 0.02
    x_end   <- 0.6
    x_step  <- 0.1
    x_expand <- c(0.005, 0.015)
    last_year <- 2015
  }
  
  df <- input_df %>%
    mutate(quarter_end = ceiling(month_end/3)) %>% 
    filter(year_end == last_year & quarter_end == 4) %>%
    filter(tau_pi_R > 0)
  
  x_axis  <- seq(0, x_end, x_step)
  y_axis  <- seq(0, y_end, y_step)
  
  print(paste(length(unique(df$permno)), mean(df$tau_pi_R, na.rm=T), median(df$tau_pi_R, na.rm=T), quantile(df$tau_pi_R, 0.95, na.rm=TRUE), max(df$tau_pi_R, na.rm=T)))
  
  p <- df %>% 
    ggplot(aes(x = tau_pi_R)) +
    geom_histogram(aes(y = stat(count) / sum(count)), bins = 50, colour = "black", fill = "steelblue", alpha = 0.2) + 
    labs(x = "Relative Price Informativeness", 
         y = "Frequency") + 
    theme_classic() +
    theme(text = element_text(size = 16)) +
    scale_x_continuous(breaks = x_axis, expand = x_expand) +
    scale_y_continuous(breaks = y_axis, expand = c(0.0, 0.0)) +
    coord_cartesian(xlim = c(0, x_end), 
                    ylim = c(0, y_end))
    
  ggsave(paste0("output/output_figures/", namefile, ".pdf"), plot = p, width = 6, height = 6, units = "in", dpi = 300)
  ggsave(paste0("output/output_figures/", namefile, ".jpeg"), plot = p, width = 6, height = 6, units = "in", dpi = 300)
  ggsave(paste0("output/output_figures/", namefile, ".eps"), plot = p, width = 6, height = 6, units = "in", dpi = 300)
    
}

histogram_fn(results_q_PS, "figure_2")
histogram_fn(results_q_PS_unlearnable, "figure_7")
histogram_fn(results_q_rolling_clean, "figure_IA10")
histogram_fn(results_q_rolling_unlearnable_clean, "figure_IA15")
histogram_fn(results_q_rolling_nocon_clean, "figure_IA17")

```

# Time Series

```{r}

fn_ts <- function(input_df, namefile) {
  df <- input_df %>%
    mutate(quarter_end = ceiling(month_end/3)) %>%
    filter(tau_pi_R > 0)
  
  # Plot 
  if(startsWith(namefile, "figure_8")){
    date_cut <- 1996
    x_start <- 1995
    x_end   <- 2017
    x_step  <- 5
    x_axis  <- seq(x_start, x_end, x_step)
    
    y_start <- 0
    y_end   <- 0.018
    y_step  <- 0.003
    y_axis  <- seq(y_start, y_end, y_step)
    
    y_start2 <- 0
    y_end2   <- 0.05
    y_step2  <- 0.01
    y_axis2 <- seq(y_start2, y_end2, y_step2)
  }else if(startsWith(namefile, "figure_IA16")){
    date_cut <- 1995
    x_start <- 1990
    x_end   <- 2017
    x_step  <- 5
    x_axis  <- seq(x_start, x_end, x_step)

    y_start <- 0
    y_end   <- 0.16
    y_step  <- 0.01
    y_axis  <- seq(y_start, y_end, y_step)
    
    y_start2 <- 0
    y_end2   <- 0.16
    y_step2  <- 0.01
    y_axis2 <- seq(y_start2, y_end2, y_step2)
  }else if(startsWith(namefile, "figure_5")){
    date_cut <- 1985
    x_start <- 1985
    x_end   <- 2017
    x_step  <- 5
    x_axis  <- seq(x_start, x_end, x_step)

    y_start <- 0
    y_end   <- 0.04
    y_step  <- 0.005
    y_axis  <- seq(y_start, y_end, y_step)
    
    y_start2 <- 0
    y_end2   <- 0.04
    y_step2  <- 0.005
    y_axis2 <- seq(y_start2, y_end2, y_step2)
  }else if(startsWith(namefile, "figure_IA13") | startsWith(namefile, "figure_IA18")){
    date_cut <- 1985
    x_start <- 1985
    x_end   <- 2017
    x_step  <- 5
    x_axis  <- seq(x_start, x_end, x_step)

    y_start <- 0
    y_end   <- 0.12
    y_step  <- 0.01
    y_axis  <- seq(y_start, y_end, y_step)
    
    y_start2 <- 0
    y_end2   <- 0.16
    y_step2  <- 0.01
    y_axis2 <- seq(y_start2, y_end2, y_step2)
  }
  
  df_rolling <- df %>% 
    group_by(year_end, quarter_end) %>% 
    summarize(across(c(tau_pi_R), 
                     list(median = ~ median(., na.rm = T),
                          mean   = ~ mean(., na.rm = T),
                          sd     = ~ sd(., na.rm = T)),
                     .names = "{col}_{fn}"),
              n_a = n(),
              .groups = "keep") %>% 
    mutate(freq = "quarterly", datefrac = year_end + quarter_end/4 - 1/8) %>% 
    filter(datefrac >= date_cut & datefrac < 2017)
  
  print(paste(mean(df_rolling$tau_pi_R_mean, na.rm=T), mean(df_rolling$tau_pi_R_median, na.rm=T), mean(df_rolling$tau_pi_R_sd, na.rm=T),length(unique(df$permno))))
  
  df_p1 <- df_rolling %>% 
    filter(datefrac >= date_cut & datefrac < 2017) %>%
    select(datefrac, tau_pi_R_median, tau_pi_R_mean)
  
  mean_label <- c(2010, max(df_p1$tau_pi_R_mean)*1.05)
  median_label <- c(2010, min(df_p1$tau_pi_R_median))
  
  p1 <- ggplot(df_p1) + 
    theme_classic() +
    geom_line(aes(x = datefrac,   y = tau_pi_R_median), color = "steelblue", size = 1.5) +
    geom_line(aes(x = datefrac,   y = tau_pi_R_mean),   color = "steelblue", size = 1.5) +
    geom_point(aes(x = datefrac,  y = tau_pi_R_median), color = "black",     size = 2) +
    geom_point(aes(x = datefrac,  y = tau_pi_R_mean),   color = "black",     size = 2) +
    geom_smooth(data = subset(df_p1, datefrac >= 1986),
                aes(x = datefrac, y = tau_pi_R_mean),   color = "red", method = lm, formula = y~x, se = FALSE, linetype = "dashed") +
    geom_smooth(data = subset(df_p1, datefrac >= 1986),
                aes(x = datefrac, y = tau_pi_R_median), color = "red", method = lm, formula = y~x, se = FALSE, linetype = "dashed") +
    theme(legend.position = "none") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(text = element_text(size = 16)) +
    labs(x = "Year", y = "Relative Price Informativeness", title = "Mean and Median") + 
    geom_text(aes(x = mean_label[1], y = mean_label[2], label = "Mean",  size = 16)) + #0.056 or 0.0605
    geom_text(aes(x = median_label[1], y = median_label[2], label = "Median",  size = 16)) + # 0.026 or 0.03
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.0,0.0)) +
    scale_x_continuous(limits = c(x_start, x_end), breaks = x_axis, expand = c(0.02,0.02))
    
  df_p2 <- df_rolling %>% 
    filter(datefrac >= date_cut & datefrac < 2017) %>%
    select(datefrac, tau_pi_R_sd)
  
  sd_label <- c(2008, min(df_p2$tau_pi_R_sd))
  
  p2 <- ggplot(df_p2) + 
    theme_classic() +
    geom_line(aes(x = datefrac,   y = tau_pi_R_sd), color = "steelblue", size = 1.2) +
    geom_point(aes(x = datefrac,  y = tau_pi_R_sd), color = "black",     size = 2) +
    geom_smooth(data = subset(df_p2, datefrac >= 1986),
                aes(x = datefrac, y = tau_pi_R_sd), color = "red", method = lm, formula = y~x, se = FALSE, linetype = "dashed") +
    theme(legend.position = "none") +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(text = element_text(size = 16)) +
    labs(x = "Year", y = "Relative Price Informativeness", title = "Standard Deviation") + 
    geom_text(aes(x = sd_label[1], y = sd_label[2], label = "Standard Deviation",  size = 16)) + # 0.066
    scale_y_continuous(limits = c(y_start2, y_end2), breaks = y_axis2, expand = c(0.0,0.0)) +
    scale_x_continuous(limits = c(x_start, x_end), breaks = x_axis, expand = c(0.02,0.02))
  
  plot_grid(p1, p2, ncol = 2)
  ggsave(paste("output/output_figures/", namefile, ".pdf", sep=""), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
  ggsave(paste("output/output_figures/", namefile, ".jpeg", sep=""), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
  ggsave(paste("output/output_figures/", namefile, ".eps", sep=""), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
}


fn_ts(results_q_PS, "figure_5")
fn_ts(results_q_PS_unlearnable, "figure_8")
fn_ts(results_q_rolling_clean, "figure_IA13")
fn_ts(results_q_rolling_unlearnable_clean, "figure_IA16")
fn_ts(results_q_rolling_nocon_clean, "figure_IA18")

```

# Cross Section Binscatter

```{r}

fn_cross_section <- function(input_df){
  df_merged <- input_df %>%
    filter(year_end >= 1985 & year_end < 2017) %>%
    filter(tau_pi_R > 0) %>%
    mutate(book = ifelse(booktomarket < 0, NA, booktomarket),
           size = log(mcap),
           analyst_count = ifelse(is.na(analyst_count), 0, analyst_count)) %>%
    drop_na()
    
  df <- df_merged %>%
    group_by(year_end) %>%
    mutate(across(c(size, turnover, book, ivol, io_share, analyst_count), list(ntile = ~ntile(., 20)), .names = "{col}_{fn}"))
  
  df_size     <- df %>% group_by(year_end, size_ntile)     %>% summarize(characteristic = mean(size),     tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = size_ntile) %>% drop_na()
  
  reg_size     <- lm(tau_pi_R ~ characteristic + factor(year_end), data = df_size)
  df_size$resid <- reg_size$residuals
  
  df <- df %>% 
    left_join(df_size %>% select(year_end, ntile, resid),
              by = c("year_end" = "year_end", "size_ntile" = "ntile"))
  df_merged$resid <- df$resid
  
  df_size     <- df %>% group_by(year_end, size_ntile)     %>% summarize(characteristic = mean(size),     tau_pi_R = mean(tau_pi_R), resid = mean(resid), var = "size", .groups = "keep") %>% rename("ntile" = size_ntile)
  df_turnover <- df %>% group_by(year_end, turnover_ntile) %>% summarize(characteristic = mean(turnover), tau_pi_R = mean(tau_pi_R), resid = mean(resid), var = "size", .groups = "keep") %>% rename("ntile" = turnover_ntile)
  df_book     <- df %>% group_by(year_end, book_ntile)     %>% summarize(characteristic = mean(book),     tau_pi_R = mean(tau_pi_R), resid = mean(resid), var = "size", .groups = "keep") %>% rename("ntile" = book_ntile)
  df_ivol     <- df %>% group_by(year_end, ivol_ntile)     %>% summarize(characteristic = mean(ivol),     tau_pi_R = mean(tau_pi_R), resid = mean(resid), var = "size", .groups = "keep") %>% rename("ntile" = ivol_ntile)
  df_io       <- df %>% group_by(year_end, io_share_ntile) %>% summarize(characteristic = mean(io_share), tau_pi_R = mean(tau_pi_R), resid = mean(resid), var = "size", .groups = "keep") %>% rename("ntile" = io_share_ntile)
  df_coverage <- df %>% group_by(year_end, analyst_count_ntile) %>% summarize(characteristic = mean(analyst_count), tau_pi_R = mean(tau_pi_R), resid = mean(resid), var = "analyst_count", .groups = "keep") %>% rename("ntile" = analyst_count_ntile)
  
  theme_ax = theme(
    axis.title.x = element_text(size = 50),
    axis.title.y = element_text(size = 50),
    plot.margin = unit(c(1,8,1,1), 'mm')
    )
  
  y_start <- 0
  y_end   <- 0.07
  y_step  <- 0.01
  y_axis = seq(y_start, y_end, y_step)
  
  p1 <- df_size %>% 
    mutate(year = as.factor(year_end)) %>% 
    ggplot(aes(x = characteristic, y = tau_pi_R)) + 
    geom_point(color = "black", fill = "orange", alpha = 0.9) + 
    geom_smooth(method = lm, se = F, color = "steelblue") +
    facet_wrap( ~ year, scales = "free", ncol = 6) +
    labs(x = "Size", 
           y = "Relative Price Informativeness") + 
    theme_classic() + theme_ax +
    theme(text = element_text(size = 30)) +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.001,0.0)) +
    scale_x_continuous(labels=function(x) sprintf("%.0f", x))
  
  p2 <- df_book %>% 
    mutate(year = as.factor(year_end)) %>%
    ggplot(aes(x = characteristic, y = tau_pi_R)) + 
    geom_point(color = "black", fill = "orange", alpha = 0.9) +
    geom_smooth(method = lm, se = F, color = "steelblue") +
    facet_wrap( ~ year, scales = "free", ncol = 6) +
    labs(x = "Value", 
           y = "Relative Price Informativeness") + 
    theme_classic() + theme_ax +
    theme(text = element_text(size = 30)) +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.001,0.0)) +
    scale_x_continuous(labels=function(x) sprintf("%.2f", x))
  
  p3 <- df_turnover %>% 
    mutate(year = as.factor(year_end)) %>%
    ggplot(aes(x = characteristic, y = tau_pi_R)) + 
    geom_point(color = "black", fill = "orange", alpha = 0.9) +
    geom_smooth(method = lm, se = F, color = "steelblue") +
    facet_wrap( ~ year, scales = "free", ncol = 6) +
    labs(x = "Turnover", 
           y = "Relative Price Informativeness") + 
    theme_classic() + theme_ax +
    theme(text = element_text(size = 30)) +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.001,0.0)) +
    scale_x_continuous(labels=function(x) sprintf("%.0f", x))
  
  p4 <- df_ivol %>% 
    mutate(year = as.factor(year_end)) %>%
    ggplot(aes(x = characteristic, y = tau_pi_R)) + 
    geom_point(color = "black", fill = "orange", alpha = 0.9) +
    geom_smooth(method = lm, se = F, color = "steelblue") +
    facet_wrap( ~ year, scales = "free", ncol = 6) +
    labs(x = "Idiosyncratic Volatility", 
           y = "Relative Price Informativeness") + 
    theme_classic() + theme_ax +
    theme(text = element_text(size = 30)) +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.001,0.0)) + 
    scale_x_continuous(labels=function(x) sprintf("%.2f", x))
  
  p5 <- df_io %>% 
    mutate(year = as.factor(year_end)) %>%
    ggplot(aes(x = characteristic, y = tau_pi_R)) + 
    geom_point(color = "black", fill = "orange", alpha = 0.9) +
    geom_smooth(method = lm, se = F, color = "steelblue") +
    facet_wrap( ~ year, scales = "free", ncol = 6) +
    labs(x = "Institutional Ownership", 
           y = "Relative Price Informativeness") + 
    theme_classic() + theme_ax +
    theme(text = element_text(size = 30)) +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.001,0.0)) +
    scale_x_continuous(labels=function(x) sprintf("%.1f", x))
  
  for(filetype in c("pdf", "jpeg", "eps")){
    ggsave(paste0("output/output_figures/figure_IA3.", filetype), plot = p1, width = 30, height = 35, units = "in", dpi = 300)
    ggsave(paste0("output/output_figures/figure_IA4.", filetype), plot = p2, width = 30, height = 35, units = "in", dpi = 300)
    ggsave(paste0("output/output_figures/figure_IA5.", filetype), plot = p3, width = 30, height = 35, units = "in", dpi = 300)
    ggsave(paste0("output/output_figures/figure_IA6.", filetype), plot = p4, width = 30, height = 35, units = "in", dpi = 300)
    ggsave(paste0("output/output_figures/figure_IA7.", filetype), plot = p5, width = 30, height = 35, units = "in", dpi = 300)
  }
  
}

fn_cross_section(results_q_PS)
  
```

# Cross Section Whisker

```{r}

fn_char <- function(input_df, namefile="") {
  
  if(startsWith(namefile, "rolling_q")){
    df_char <- input_df  %>% filter(year_end >= 1985) %>%
    left_join(df_q %>% select(permno, year, month, sp500, exchcd ,sic1),
              by = c("permno" = "permno", "year_end" = "year", "month_end" = "month")) %>% 
    mutate(sp500 = factor(sp500)) %>%
    filter(sic1 != "Public administration")
  }else{
    df_char <- input_df %>% filter(tau_pi_R > 0) %>%
      mutate(tau_pi_R=Winsorize(tau_pi_R, val = quantile(tau_pi_R, probs = c(0, 0.99), na.rm = FALSE))) %>%
      mutate(sp500 = factor(sp500)) %>%
      filter(sic1 != "Public administration")
  }
  
  if(namefile == ""){
    y_start <- -0.04
    y_end   <- 0.06
    y_step  <- 0.01
    y_expand <- c(0.001, 0.001)
  }else if(namefile == "unlearnable"){
    y_start <- -0.02
    y_end   <- 0.08
    y_step  <- 0.02
    y_expand <- c(0.001, 0.001)
  }else if(startsWith(namefile, "rolling_q")){
    y_start <- -0.2
    y_end   <- 0.7
    y_step  <- 0.1
    y_expand <- c(0.001, 0.001)
  }else{
    y_start <- -0.04
    y_end   <- 0.06
    y_step  <- 0.01
    y_expand <- c(0.001, 0.001)
  }
  
  reg_exchcd <- lm(tau_pi_R ~ exchcd + factor(year_end), data = df_char)
  reg_sp500  <- lm(tau_pi_R ~ sp500  + factor(year_end), data = df_char)
  reg_sic1   <- lm(tau_pi_R ~ sic1   + factor(year_end), data = df_char)
  
  exchcd <- summary(reg_exchcd)$coefficients[1:3,] %>% data.frame() %>% select(-starts_with("P"))
  sp500  <- summary(reg_sp500)$coefficients[1:2,]  %>% data.frame() %>% select(-starts_with("P"))
  sic1   <- summary(reg_sic1)$coefficients[1:7,]   %>% data.frame() %>% select(-starts_with("P"))
  
  reg_exchcd_fe <- lm(tau_pi_R ~ factor(year_end), data = df_char)
  reg_sp500_fe  <- lm(tau_pi_R ~ factor(year_end), data = df_char)
  reg_sic1_fe   <- lm(tau_pi_R ~ factor(year_end), data = df_char)
  
  # Plots
  y_axis  <- seq(y_start, y_end, y_step)
  
  df_exchcd_fe <- data.frame(reg_exchcd_fe$residuals, df_char$exchcd) %>% 
    rename(resid = reg_exchcd_fe.residuals, exchcd = df_char.exchcd) %>% 
    mutate(exchcd = factor(exchcd, levels = c("NYSE", "NASDAQ", "AMEX")))
    
  pc1 <- df_exchcd_fe %>% 
    ggplot(aes(x = exchcd, y = resid)) + 
    geom_boxplot(fill = "white", colour = "steelblue", alpha = 0.3, outlier.colour = "steelblue", outlier.size = 0.1 ) +
    theme_classic() +
    labs(x = "", y = "Price Informativeness (residual)", title = "Exchange") +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = y_expand) +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(text = element_text(size = 16))
  
  df_sp500_fe <- data.frame(reg_sp500_fe$residuals, df_char$sp500) %>%
    rename(resid = reg_sp500_fe.residuals, sp500 = df_char.sp500) %>% 
    mutate(sp500 = fct_recode(sp500, "S&P 500" = "1", "No S&P 500" = "0")) %>% 
    mutate(sp500 = factor(sp500, levels = c("S&P 500", "No S&P 500")))
                                 
  pc2 <- df_sp500_fe %>% 
    ggplot(aes(x = sp500, y = resid)) + 
    geom_boxplot(fill = "white", colour = "steelblue", alpha = 0.3, outlier.colour = "steelblue", outlier.size = 0.1 ) +
    theme_classic() +
    labs(x = "", y = "Price Informativeness (residual)", title = "S&P 500") +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = y_expand) +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(text = element_text(size = 16))
  
  df_charac_fe <- data.frame(reg_sic1_fe$residuals, df_char$sic1) %>%
    rename(resid = reg_sic1_fe.residuals, sic1 = df_char.sic1) %>% 
    mutate(sic1     = fct_recode(sic1,
                                  "Agric./Mining/Constr." = "Agriculture/Mining/Construction",
                                  "Transportation"        = "Transportation/Communications",
                                  "Wholesale/Retail"      = "Wholesale/Retail trade",
                                  "Public Admin."         = "Public administration"))
  
  pc3 <- df_charac_fe %>% 
    ggplot(aes(x = sic1, y = resid)) + 
    geom_boxplot(fill = "white", colour = "steelblue", alpha = 0.3, outlier.colour = "steelblue", outlier.size = 0.1 ) +
    theme_classic() +
    labs(x = "", y = "Price Informativeness (residual)", title = "Sector") +
    scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = y_expand) +
    theme(axis.text.x = element_text(angle = 50, hjust = 1)) +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(text = element_text(size = 16))
  
  p <- plot_grid(pc1, pc2, ncol = 2)
  
  for(filetype in c("pdf", "jpeg", "eps")){
    if(namefile == "unlearnable"){
      ggsave(paste0("output/output_figures/figure_IA8.", filetype), plot = p, width = 10, height = 5, units = "in", dpi = 300)
      ggsave(paste0("output/output_figures/figure_IA9.", filetype), plot = pc3, width = 5, height = 5, units = "in", dpi = 300)
    }else if(namefile == "rolling_q"){
      ggsave(paste0("output/output_figures/figure_IA11.", filetype), plot = p, width = 10, height = 5, units = "in", dpi = 300)
      ggsave(paste0("output/output_figures/figure_IA12.", filetype), plot = pc3, width = 5, height = 5, units = "in", dpi = 300)
    }else if(namefile == "rolling_q_nocon"){
      ggsave(paste0("output/output_figures/figure_IA19.", filetype), plot = p, width = 10, height = 5, units = "in", dpi = 300)
      ggsave(paste0("output/output_figures/figure_IA20.", filetype), plot = pc3, width = 5, height = 5, units = "in", dpi = 300)
    }else{
      ggsave(paste0("output/output_figures/figure_3.", filetype), plot = p, width = 10, height = 5, units = "in", dpi = 300)
      ggsave(paste0("output/output_figures/figure_4.", filetype), plot = pc3, width = 5, height = 5, units = "in", dpi = 300)
    }
  }
}

fn_char(results_q_PS)
fn_char(results_q_PS_unlearnable, "unlearnable")
fn_char(results_q_rolling_clean, "rolling_q")
fn_char(results_q_rolling_nocon_clean, "rolling_q_nocon")

```

# Share Negative
```{r}

df <- results_q_PS %>%
  mutate(pos_PR = tau_pi_R > 0) %>%
  filter(year >= 1985) %>%
  group_by(year) %>%
  summarize(share_pos_PR = mean(pos_PR))

p <- ggplot(df) + 
  theme_classic() +
  geom_line(aes(x = year,   y = share_pos_PR), color = "steelblue", size = 1.5) +
  geom_point(aes(x = year,  y = share_pos_PR), color = "black",     size = 2) +
  theme(legend.position = "none") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(text = element_text(size = 16)) +
  labs(x = "Year", y = "Share", title = "Share Positive Relative Price Informativeness") +
  scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, .1), expand = c(0.0,0.0)) +
  scale_x_continuous(limits = c(1985, 2016), breaks = seq(1980, 2015, 5), expand = c(0.02,0.02))
ggsave(paste0("output/output_figures/figure_IA2.pdf"), plot = p, width = 6, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA2.jpeg"), plot = p, width = 6, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA2.eps"), plot = p, width = 6, height = 6, units = "in", dpi = 300)
```

# Time Series Top/Bottom Half, PS

```{r}

df_merged <- results_q_PS %>%
    filter(tau_pi_R > 0) %>%
    filter(year_end >= 1985 & year_end < 2017) %>%
    mutate(book = ifelse(booktomarket < 0, NA, booktomarket),
           size = log(mcap),
           analyst_count = ifelse(is.na(analyst_count), 0, analyst_count))

df_noncovered <- df_merged %>% 
  filter(analyst_count == 0) %>%
  group_by(year_end) %>% summarize(characteristic = mean(analyst_count), tau_pi_R_sd = sd(tau_pi_R),  tau_pi_R = mean(tau_pi_R)) %>%
  mutate(ntile = 0)

df_covered <- df_merged %>% 
  filter(analyst_count > 0) %>%
  mutate(across(c(analyst_count), list(ntile = ~ntile(., 2)), .names = "{fn}")) %>%
  group_by(year_end, ntile) %>% summarize(characteristic = mean(analyst_count), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R))

df_coverage <- rbind(df_covered, df_noncovered)

df <- df_merged %>%
  mutate(across(c(size, turnover, book, ivol, io_share), list(ntile = ~ntile(., 2)), .names = "{col}_{fn}"))
  
df_size     <- df %>% group_by(year_end, size_ntile)     %>% summarize(characteristic = mean(size), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = size_ntile)
df_turnover <- df %>% group_by(year_end, turnover_ntile) %>% summarize(characteristic = mean(turnover), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = turnover_ntile)
df_book     <- df %>% group_by(year_end, book_ntile)     %>% summarize(characteristic = mean(book), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = book_ntile) 
df_ivol     <- df %>% group_by(year_end, ivol_ntile)     %>% summarize(characteristic = mean(ivol), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = ivol_ntile)
df_io       <- df %>% group_by(year_end, io_share_ntile) %>% summarize(characteristic = mean(io_share), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = io_share_ntile)

df_size_plot     <- df_size     %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_")%>% ungroup()
df_turnover_plot <- df_turnover %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_book_plot     <- df_book     %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_ivol_plot     <- df_ivol     %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_io_plot       <- df_io       %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_coverage_plot <- df_coverage%>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()


texts <- list()
x_start <- 1985
x_end   <- 2017
x_step  <- 5
x_axis  <- seq(x_start, x_end, x_step)

y_start <- 0.0
y_end   <- 0.05
y_step  <- 0.01
texts[[1]] <- c(2003, 0.045, 2003, 0.01)
texts[[2]] <- c(2003, 0.045, 2007, 0.01)
texts[[3]] <- c(2005, 0.01, 1995, 0.035)
texts[[4]] <- c(1990, 0.01, 1990, 0.03)
texts[[5]] <- c(2003, 0.045, 2003, 0.01)
texts[[6]] <- c(1992, 0.045, 2013.5, 0.03, 2007, 0.012)

y_axis  <- seq(y_start, y_end, y_step)
datasets <- list(df_size_plot, df_turnover_plot, df_book_plot,
                 df_ivol_plot, df_io_plot, df_coverage_plot)
titles <- c("Size", "Turnover", "Value", "Idiosyncratic Volatility",
            "Instituitional Ownership", "Analyst Coverage")
plots <- list()

for(p in 1:6){
  temp_plot <- local({
        p <- p
        ggplot(datasets[[p]]) + 
          theme_classic() +
          geom_line(aes(x = year_end,   y = var1), color = "steelblue", size = 1.5) +
          geom_line(aes(x = year_end,   y = var2), color = "red", size = 1.5, linetype = "dashed") +
          labs(x = "Year", y = "Relative Price Informativeness", title = titles[p]) +
          theme(legend.position = "none") +
          theme(plot.title = element_text(hjust = 0.5, size = 16)) +
          scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.0,0.0)) +
          scale_x_continuous(limits = c(x_start, x_end), breaks = x_axis, expand = c(0.02,0.02)) +
          geom_text(aes(x = texts[[p]][1], y = texts[[p]][2], label = "Top Half",    size = 15)) +
          geom_text(aes(x = texts[[p]][3], y = texts[[p]][4], label = "Bottom Half",  size = 15))
    })
  if(length(texts[[p]]) > 4){
    temp_plot = temp_plot + 
                geom_line(aes(x = year_end,   y = var0), color = "darkgreen", size = 1.5, linetype = "dotted") +
                geom_text(aes(x = texts[[p]][5], y = texts[[p]][6], label = "No Coverage",  size = 15))
  }
  plots[[p]] = temp_plot
}

top_row    = plot_grid(plots[[1]], plots[[2]], plots[[3]], ncol=3)
middle_row = plot_grid(plots[[4]], plots[[5]], plots[[6]], ncol=3)

plot_grid(top_row, middle_row, ncol=1)
ggsave(paste0("output/output_figures/figure_6a.pdf"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_6a.jpeg"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_6a.eps"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)


df_size_plot     <- df_size     %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_turnover_plot <- df_turnover %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_book_plot     <- df_book     %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_ivol_plot     <- df_ivol     %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_io_plot       <- df_io       %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_coverage_plot <- df_coverage%>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")

texts <- list()
x_start <- 1985
x_end   <- 2017
x_step  <- 5
x_axis  <- seq(x_start, x_end, x_step)

y_start <- 0.00
y_end   <- 0.04
y_step  <- 0.01
texts[[1]] <- c(2003, 0.022, 2003, 0.005)
texts[[2]] <- c(2000, 0.025, 2003, 0.007)
texts[[3]] <- c(1997, 0.005, 1997, 0.025)
texts[[4]] <- c(1997, 0.025, 2006, 0.008)
texts[[5]] <- c(2003, 0.023, 1990, 0.02)
texts[[6]] <- c(2000, 0.03, 2010, 0.03, 1990, 0.02)

y_axis  <- seq(y_start, y_end, y_step)
datasets <- list(df_size_plot, df_turnover_plot, df_book_plot,
                 df_ivol_plot, df_io_plot, df_coverage_plot)
titles <- c("Size", "Turnover", "Value", "Idiosyncratic Volatility",
            "Instituitional Ownership", "Analyst Coverage")
plots <- list()

for(p in 1:6){
  temp_plot <- local({
        p <- p
        ggplot(datasets[[p]]) + 
          theme_classic() +
          geom_line(aes(x = year_end,   y = var1), color = "steelblue", size = 1.5) +
          geom_line(aes(x = year_end,   y = var2), color = "red", size = 1.5, linetype = "dashed") +
          labs(x = "Year", y = "Relative Price Informativeness SD", title = titles[p]) +
          theme(legend.position = "none") +
          theme(plot.title = element_text(hjust = 0.5, size = 16)) +
          scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.0,0.0)) +
          scale_x_continuous(limits = c(x_start, x_end), breaks = x_axis, expand = c(0.02,0.02)) +
          geom_text(aes(x = texts[[p]][1], y = texts[[p]][2], label = "Top Half",    size = 15)) +
          geom_text(aes(x = texts[[p]][3], y = texts[[p]][4], label = "Bottom Half",  size = 15))
    })
  if(length(texts[[p]]) > 4){
    temp_plot = temp_plot + 
                geom_line(aes(x = year_end,   y = var0), color = "darkgreen", size = 1.5, linetype = "dotted") +
                geom_text(aes(x = texts[[p]][5], y = texts[[p]][6], label = "No Coverage",  size = 15))
  }
  plots[[p]] = temp_plot
}

top_row    = plot_grid(plots[[1]], plots[[2]], plots[[3]], ncol=3)
middle_row = plot_grid(plots[[4]], plots[[5]], plots[[6]], ncol=3)

plot_grid(top_row, middle_row, ncol=1)
ggsave(paste0("output/output_figures/figure_6b.pdf"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_6b.jpeg"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_6b.eps"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)

```
# Time Series Top/Bottom Half

```{r}

df_merged <- results_q_rolling_clean %>% #
    left_join(df_q %>% select(permno, year, month, mcap, booktomarket, turnover, analyst_count),
                           by = c("permno" = "permno", "year_end" = "year", "month_end" = "month")) %>%
    left_join(ivol,        by = c("permno" = "permno", "year_end" = "year", "month_end" = "month")) %>%
    left_join(df_io_share, by = c("permno" = "permno", "year_end" = "year", "month_end" = "month")) %>%
    filter(year_end >= 1985 & year_end < 2017) %>%
    mutate(book = ifelse(booktomarket < 0, NA, booktomarket),
           size = log(mcap),
           analyst_count = ifelse(is.na(analyst_count), 0, analyst_count)) %>%
    drop_na()

df_noncovered <- df_merged %>% 
  filter(analyst_count == 0) %>%
  group_by(year_end) %>% summarize(characteristic = mean(analyst_count), tau_pi_R_sd = sd(tau_pi_R),  tau_pi_R = mean(tau_pi_R)) %>%
  mutate(ntile = 0)

df_covered <- df_merged %>% 
  filter(analyst_count > 0) %>%
  mutate(across(c(analyst_count), list(ntile = ~ntile(., 2)), .names = "{fn}")) %>%
  group_by(year_end, ntile) %>% summarize(characteristic = mean(analyst_count), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R))

df_coverage <- rbind(df_covered, df_noncovered)

df <- df_merged %>%
  mutate(across(c(size, turnover, book, ivol, io_share), list(ntile = ~ntile(., 2)), .names = "{col}_{fn}"))
  
df_size     <- df %>% group_by(year_end, size_ntile)     %>% summarize(characteristic = mean(size), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = size_ntile)
df_turnover <- df %>% group_by(year_end, turnover_ntile) %>% summarize(characteristic = mean(turnover), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = turnover_ntile)
df_book     <- df %>% group_by(year_end, book_ntile)     %>% summarize(characteristic = mean(book), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = book_ntile) 
df_ivol     <- df %>% group_by(year_end, ivol_ntile)     %>% summarize(characteristic = mean(ivol), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = ivol_ntile)
df_io       <- df %>% group_by(year_end, io_share_ntile) %>% summarize(characteristic = mean(io_share), tau_pi_R_sd = sd(tau_pi_R), tau_pi_R = mean(tau_pi_R), var = "size", .groups = "keep") %>% rename("ntile" = io_share_ntile)

df_size_plot     <- df_size     %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_")%>% ungroup()
df_turnover_plot <- df_turnover %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_book_plot     <- df_book     %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_ivol_plot     <- df_ivol     %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_io_plot       <- df_io       %>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()
df_coverage_plot <- df_coverage%>% select(year_end, ntile, tau_pi_R) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R, names_prefix = "var", names_sep = "_") %>% ungroup()


texts <- list()
x_start <- 1985
x_end   <- 2017
x_step  <- 5
x_axis  <- seq(x_start, x_end, x_step)

y_start <- 0.045
y_end   <- 0.145
y_step  <- 0.01
texts[[1]] <- c(2003, 0.115, 2003, 0.055)
texts[[2]] <- c(2003, 0.125, 2007, 0.055)
texts[[3]] <- c(2005, 0.06, 1995, 0.115)
texts[[4]] <- c(1990, 0.105, 2003, 0.055)
texts[[5]] <- c(2003, 0.125, 2003, 0.055)
texts[[6]] <- c(2005, 0.135, 1990, 0.12, 2007, 0.055)

y_axis  <- seq(y_start, y_end, y_step)
datasets <- list(df_size_plot, df_turnover_plot, df_book_plot,
                 df_ivol_plot, df_io_plot, df_coverage_plot)
titles <- c("Size", "Turnover", "Value", "Idiosyncratic Volatility",
            "Instituitional Ownership", "Analyst Coverage")
plots <- list()

for(p in 1:6){
  temp_plot <- local({
        p <- p
        ggplot(datasets[[p]]) + 
          theme_classic() +
          geom_line(aes(x = year_end,   y = var1), color = "steelblue", size = 1.5) +
          geom_line(aes(x = year_end,   y = var2), color = "red", size = 1.5, linetype = "dashed") +
          labs(x = "Year", y = "Relative Price Informativeness", title = titles[p]) +
          theme(legend.position = "none") +
          theme(plot.title = element_text(hjust = 0.5, size = 16)) +
          scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.0,0.0)) +
          scale_x_continuous(limits = c(x_start, x_end), breaks = x_axis, expand = c(0.02,0.02)) +
          geom_text(aes(x = texts[[p]][1], y = texts[[p]][2], label = "Top Half",    size = 15)) +
          geom_text(aes(x = texts[[p]][3], y = texts[[p]][4], label = "Bottom Half",  size = 15))
    })
  if(length(texts[[p]]) > 4){
    temp_plot = temp_plot + 
                geom_line(aes(x = year_end,   y = var0), color = "darkgreen", size = 1.5, linetype = "dotted") +
                geom_text(aes(x = texts[[p]][5], y = texts[[p]][6], label = "No Coverage",  size = 15))
  }
  plots[[p]] = temp_plot
}

top_row    = plot_grid(plots[[1]], plots[[2]], plots[[3]], ncol=3)
middle_row = plot_grid(plots[[4]], plots[[5]], plots[[6]], ncol=3)

plot_grid(top_row, middle_row, ncol=1)
ggsave(paste0("output/output_figures/figure_IA14a.pdf"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA14a.jpeg"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA14a.eps"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)


df_size_plot     <- df_size     %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_turnover_plot <- df_turnover %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_book_plot     <- df_book     %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_ivol_plot     <- df_ivol     %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_io_plot       <- df_io       %>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")
df_coverage_plot <- df_coverage%>% select(year_end, ntile, tau_pi_R_sd) %>% pivot_wider(names_from = ntile, values_from = tau_pi_R_sd, names_prefix = "var", names_sep = "_")

texts <- list()
x_start <- 1985
x_end   <- 2017
x_step  <- 5
x_axis  <- seq(x_start, x_end, x_step)

y_start <- 0.04
y_end   <- 0.16
y_step  <- 0.01
texts[[1]] <- c(2003, 0.14, 2003, 0.07)
texts[[2]] <- c(2003, 0.14, 2003, 0.07)
texts[[3]] <- c(1997, 0.06, 1997, 0.13)
texts[[4]] <- c(1997, 0.13, 2003, 0.065)
texts[[5]] <- c(2003, 0.14, 2003, 0.07)
texts[[6]] <- c(1995, 0.14, 2003, 0.116, 2007, 0.07)

y_axis  <- seq(y_start, y_end, y_step)
datasets <- list(df_size_plot, df_turnover_plot, df_book_plot,
                 df_ivol_plot, df_io_plot, df_coverage_plot)
titles <- c("Size", "Turnover", "Value", "Idiosyncratic Volatility",
            "Instituitional Ownership", "Analyst Coverage")
plots <- list()

for(p in 1:6){
  temp_plot <- local({
        p <- p
        ggplot(datasets[[p]]) + 
          theme_classic() +
          geom_line(aes(x = year_end,   y = var1), color = "steelblue", size = 1.5) +
          geom_line(aes(x = year_end,   y = var2), color = "red", size = 1.5, linetype = "dashed") +
          labs(x = "Year", y = "Relative Price Informativeness SD", title = titles[p]) +
          theme(legend.position = "none") +
          theme(plot.title = element_text(hjust = 0.5, size = 16)) +
          scale_y_continuous(limits = c(y_start, y_end), breaks = y_axis, expand = c(0.0,0.0)) +
          scale_x_continuous(limits = c(x_start, x_end), breaks = x_axis, expand = c(0.02,0.02)) +
          geom_text(aes(x = texts[[p]][1], y = texts[[p]][2], label = "Top Half",    size = 15)) +
          geom_text(aes(x = texts[[p]][3], y = texts[[p]][4], label = "Bottom Half",  size = 15))
    })
  if(length(texts[[p]]) > 4){
    temp_plot = temp_plot + 
                geom_line(aes(x = year_end,   y = var0), color = "darkgreen", size = 1.5, linetype = "dotted") +
                geom_text(aes(x = texts[[p]][5], y = texts[[p]][6], label = "No Coverage",  size = 15))
  }
  plots[[p]] = temp_plot
}

top_row    = plot_grid(plots[[1]], plots[[2]], plots[[3]], ncol=3)
middle_row = plot_grid(plots[[4]], plots[[5]], plots[[6]], ncol=3)

plot_grid(top_row, middle_row, ncol=1)
ggsave(paste0("output/output_figures/figure_IA14b.pdf"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA14b.jpeg"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)
ggsave(paste0("output/output_figures/figure_IA14b.eps"), plot = last_plot(), width = 13, height = 6, units = "in", dpi = 300)

```